﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace HDJ.Framework.Core
{
  public  class AssetsManifestManager
    {
        public const string c_ManifestFileName = "StreamingAssets";
        static bool s_isInit = false;
        static AssetBundleManifest s_manifest;


        //static Dictionary<string, bool> s_relayData = new Dictionary<string, bool>();

        static void Initialize()
        {
            if (!s_isInit)
            {
                s_isInit = true;
                LoadAssetsManifest();
            }
        }

        public static void LoadAssetsManifest()
        {
            string platformFolderName = UnityExtendUtils.GetPlatformFolderName(Application.platform);
            //主动添加的_AssetBundleManifest，拼接名字，防止重名
            string path = AssetsPathController.GetPath(platformFolderName + "_AssetBundleManifest");
            path = PathUtils.RemoveExtension(path);
            Debug.Log(path);
            AssetBundle resA = AssetBundle.LoadFromFile(path);
            s_manifest = resA.LoadAllAssets()[0] as AssetBundleManifest;
            

            resA.Unload(false);
            LoadDependenciePaths();
        }

        #region New Res Use
        private static Dictionary<string, string[]> dependenciePathsDic = new Dictionary<string, string[]>();

        public static Dictionary<string, string[]> GetDependencieNamesDic()
        {
            Dictionary<string, string[]> dic = new Dictionary<string, string[]>();

            foreach (var item in dependenciePathsDic)
            {
                List<string> names = new List<string>();
                foreach (var pathArr in item.Value)
                {
                    string name = PathUtils.GetFileName(pathArr);
                    names.Add(name);
                }

                dic.Add(PathUtils.GetFileName(item.Key), names.ToArray());
            }

            return dic;
        }

        //储存含有依赖的资源的路径列表
        private static List<string> hasDependenciesPathList = new List<string>();
        private static void LoadDependenciePaths()
        {
            dependenciePathsDic.Clear();

            hasDependenciesPathList.Clear();

            string[] sArr = s_manifest.GetAllAssetBundles();
            for (int i = 0; i < sArr.Length; i++)
            {
                string assetPath = sArr[i];
                string assetName = Path.GetFileNameWithoutExtension(assetPath);
                string[] dependenPaths = s_manifest.GetDirectDependencies(assetPath);

                string[] dependens = new string[dependenPaths.Length];
                for (int j = 0; j < dependenPaths.Length; j++)
                {
                    dependens[j] = Path.GetFileNameWithoutExtension(dependenPaths[j]);
                }
                dependenciePathsDic.Add(assetName, dependens);

            }

            foreach (var assetPath in dependenciePathsDic.Keys)
            {
                bool hasDep = false;
                foreach (var depList in dependenciePathsDic.Values)
                {
                    foreach (var item in depList)
                    {
                        if (item == assetPath)
                        {
                            hasDep = true;
                            hasDependenciesPathList.Add(assetPath);
                            break;
                        }
                    }
                    if (hasDep)
                    {
                        break;
                    }
                }
                //if (!hasDep)
                //{
                //    Debug.Log("没有依赖：" + assetPath);
                //}
            }

        }
        public static string[] GetAllDependenciesPaths(string name)
        {
            if (!s_isInit)
            {
                Initialize();
            }
            if (dependenciePathsDic.Count == 0)
                return new string[0];

            if (dependenciePathsDic.ContainsKey(name))
            {
                return dependenciePathsDic[name];
            }
            else
            {
                Debug.LogError("没找到依赖 GetAllDependenciesName.Name :" + name + " dependencieNamesDic=>" + dependenciePathsDic.Count);
                return new string[0];
            }
        }
        /// <summary>
        /// 是否被依赖或依赖别人
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool IsHaveDependencies(string name)
        {
            if (!s_isInit)
            {
                Initialize();
            }
            if (hasDependenciesPathList.Contains(name))
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}
